#include "project.h"

int main(int argc, const char *argv[])
{	

    if(argc < 3){
		printf("请输入服务器IP和端口号\n");
		return -1;
	}
	s_fd = tcp_net_init(argv[1],argv[2]);
	if(-1 == s_fd){
		return -1;
	}
    printf("连接中...\n");   
    //连接connect
    if (connect(s_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1){
        ERR_LOG("connect error");
    }
    printf("连接成功\n");

	if (-1 == open_drive_fd()){
		close(s_fd);
		return -1;
	}

    strcpy(msg.user.ID, "zhangsan");
    strcpy(msg.user.password, "123456");
    send(s_fd, &msg, sizeof(msg_t), 0);
    recv(s_fd, &msg, sizeof(msg_t), 0);

	printf("登录标识：%d\n",msg.user.flags);

	int ret;
    if (1 == msg.user.flags){
		pthread_t tid;
        /*创建维护环境线程*/
		pthread_create(&tid,NULL,hold_envthread,NULL);
		
        /*等待用户下发指令，执行指令，返回结果*/
		while(1){
			memset(&msg, 0, sizeof(msg_t));
			ret = recv(s_fd, &msg,sizeof(msg_t),0);
            if (-1 == ret){
                printf("recv error");
                break;
            }else if(0 == ret){
				break;
			}
			printf("接收到的标识：%d\n", msg.commd);
			switch(msg.commd){
				case 1:
					/*获取环境数据*/
					do_getenv(&msg);
					break;
				case 2:
					/*设置阈值*/
					do_setlimit(&msg);
					break;
				case 3:
					/*控制设备*/
					do_ctrldev(&msg);
					break;
				default:
					break;
			}

		}
        //关闭
        close(s_fd);
        return 0;
    }else{
        close(s_fd);
        return -1;
    }
    return 0;
}